package com.lagou.module01.servlet;

import com.lagou.module01.bean.Student;
import com.lagou.module01.service.imp.StudentServiceImp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: xyk
 * @Date: Create in 18:00 2021/3/20
 * @Description:
 */
@WebServlet(name = "DeleteStudentServlet", urlPatterns = "/deleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        // service层获取学员信息
        StudentServiceImp studentServiceImp = new StudentServiceImp();

        String param = request.getParameter("ids");
        String[] ids = param.split(";");

        try {
            for (String id :ids){
                if (id.equals("")){
                    continue;
                }
                studentServiceImp.deleteStudentsService(id);
            }
        }
        catch (Exception e){
            out.print(e);
        }
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/pageQueryServlet");
        requestDispatcher.forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
